<?

class Rename extends Button {

    function render($sel) {
        if ($sel->object != '') {
            if ($sel->isFileSelected()) {
                return $this->renderDialogButton('rename','Rename','Rename file');
            } else {
                return $this->renderDialogButton('rename','Rename','Rename directory');
            }
        } else {
            return $this->renderDisabledButton('rename','Rename');
        }

    }

    function action($sel) {
        $DENIED_EXTENSIONS = Config::getValue('limits','DENIED_EXTENSIONS');
        if ($sel->isFileSelected()) {
            $file = $sel->getSelected();
            $name = basename($_POST['newname']);
            $ext = substr($name,strrpos($name,'.')+1);
            if (!in_array($ext,$DENIED_EXTENSIONS)) {
                rename($file->name,dirname($file->name).'/'.$name);
                header('Location: '.Config::getMsgLink($_GET['path'],$name,MESSAGE_INFO,'Successfully renamed: <b>'.basename($file->name).'</b> -> <b>'.$name.'</b>'));
            } else {
                header('Location: '.Config::getMsgLink($_GET['path'],$_GET['object'],MESSAGE_ERROR,'File extension <b>'.$ext.'</b> is forbidden to use!'));
            }
            exit;
        } else {
            $file = $sel->getSelected();
            $name = basename($_POST['newname']);
            rename($file->name,dirname($file->name).'/'.$name);
            header('Location: '.Config::getLink(str_replace($_GET['path'],basename($_GET['path']),$name),MESSAGE_INFO,'Successfully renamed: <b>'.basename($file->name).'</b> -> <b>'.$name.'</b>'));
            exit;
        }
    }

    function renderDialog($sel) {
        $d = $sel->getSelected();
        $name = basename($d->name);
        if (($sel->getCurrentDirNameRel() != '') || $sel->isFileSelected()) {
            if ($sel->isFileSelected()) {
                $link = Config::getLink($_GET['path'],$_GET['object'],'action=rename');
            } else {
                $link = Config::getLink($_GET['path'],'','action=rename');
            }
        } else {
            $link = '#';
        }
        return '<form method="POST" action="'.$link.'"><b>Current name: </b> '.$name.'&nbsp;&nbsp;|&nbsp;&nbsp;<b>New name: </b> <input type="text" name="newname" value="'.$name.'"><input type="submit" value="Rename"></form>';
    }

}

?>